package com.atguigu.add;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 集齐七颗龙珠
 *
 * @author sx
 * @date 2022-02-19 15:36
 */
public class CyclicBarrierTest {
    public static void main(String[] args) {
        //计数器
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("成功召唤神龙！");
        });

        for (int i = 1; i <= 7; i++) {
            final int temp = i;
            //lambda表达式操作不到 i
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+" 收集"+temp+"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
